﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = txtPath.Text;//文件路径
            string content = txtContent.Text;//内容
            try
            {
                if (path == "")
                {
                    MessageBox.Show("请输入文件路径");
                    return;
                }
                FileStream myFs = new FileStream(path, FileMode.Create);
                StreamWriter mySw = new StreamWriter(myFs, Encoding.Unicode);
                mySw.Write(content);
                mySw.Close();
                myFs.Close();
                MessageBox.Show("写入成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("写入失败");
            }
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.ShowDialog();
            string path = openFileDialog1.FileName;
            if (path == "")
            {
                MessageBox.Show("请选择文件");
                return;
            }
            string fileType = path.Substring(path.LastIndexOf("."));
            if (fileType != ".txt")
            {
                MessageBox.Show("请选择文本文件");
                return;
            }
            string content = "";
            FileStream myFs = new FileStream(path, FileMode.Open);
            StreamReader mySr = new StreamReader(myFs, Encoding.Unicode);
            content = mySr.ReadToEnd();
            txtContent.Text = content;
            mySr.Close();
            myFs.Close();

        }
    }
}
